// pages/last/last.js
const data = require("../../../testdata.js");
const net = require("../../../utils/net.js");
const gp = getApp().globalData;
var posting = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:1,
    list: [],
    page:1,
    nomore:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type
    })
    var title = "近期到期";
    switch(options.type*1){
      case 1:title="近期到期";break;
      case 2: title = "次数耗尽"; break;
      case 3: title = "金额耗尽"; break;
      case 4: title = "长时间未到场"; break;
      case 5: title = "长时间未跟进"; break;
    }
    wx.setNavigationBarTitle({
      title: title,
    })
    this.getdata(options.type);
  },
  getdata(type){
    var that = this;
    if (that.data.nomore){
      that.dialog.showDialog({
        msg: '没有更多'
      })
      return;
    }
    if (posting) return;
    posting = true;
    wx.showLoading({})
    net.request({
      url: "/MemberList",
      data: {
        id: gp.id,
        shop_id: gp.shop_id,
        search_type: type,
        page: that.data.page
      },
      success(res) {
        var dt = res.data;
        var list = that.data.list;
        if (dt.flag) {
          if(list.length>0){
            for(var i=0;i<dt.data.length;++i){
              list.push(dt.data[i]);
            }
          }else{
            list = dt.data;
          }
          that.setData({
            list: list,
            page: that.data.page + 1
          })
          wx.hideLoading();
        } else {
          if (dt.msg.indexOf("没有更多")>=0){
            that.setData({
              nomore:true,
              list: list
            })
          }
          wx.hideLoading();
          that.dialog.showDialog({
            msg: dt.msg
          })
        }
      }, fail(res) {
        wx.hideLoading();
      }, complete() {
        posting = false;
      }
    })
  },
  refresh(){
    console.log("refresh");
    this.getdata();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady(){
    this.dialog = this.selectComponent("#dialog");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})